package testxa;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import com.microsoft.sqlserver.jdbc.SQLServerXADataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.sql.XAConnection;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;

/**
 *
 * @author Tuandn
 */
public class TestXA {

    public static void main(String[] args) throws Exception {

        // Create variables for the connection string.
        final String prefix = "jdbc:sqlserver://";
        final String serverName = "TUAN-PC";
        final int portNumber = 1433;
        final String databaseName = "demospring";
        final String user = "sa";
        final String password = "sa";
        final String connectionUrl = prefix + serverName + ":" + portNumber
                + ";databaseName=" + databaseName + ";user=" + user + ";password=" + password;

        try {
            // Establish the connection.
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection(connectionUrl);

            // Create a test table.
            Statement stmt = con.createStatement();
            ResultSet tableList = stmt.executeQuery("SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[XAMin]') AND type in (N'U')");
            if (!tableList.next()) {
                try {
                    stmt.executeUpdate("DROP TABLE XAMin");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                stmt.executeUpdate("CREATE TABLE XAMin (f1 int, f2 varchar(max))");
            }
            stmt.close();
            con.close();

            // Create the XA data source and XA ready connection.
            SQLServerXADataSource ds = new SQLServerXADataSource();
            ds.setUser(user);
            ds.setPassword(password);
            ds.setServerName(serverName);
            ds.setPortNumber(portNumber);
            ds.setDatabaseName(databaseName);
            XAConnection xaCon = ds.getXAConnection();
            con = xaCon.getConnection();

            // Get a unique Xid object for testing.
            XAResource xaRes = null;
            Xid xid = null;
            xid = XidImpl.getUniqueXid(1);

            // Cleanup.
            con.close();
            xaCon.close();

            // Open a new connection and read back the record to verify that it worked.
            con = null;
            con = DriverManager.getConnection(connectionUrl);
            ResultSet rs = con.createStatement().executeQuery("SELECT * FROM XAMin");
            while (rs.next()) {
                System.out.println("Read -> xid = " + rs.getString(2));
            }
            rs.close();
            con.close();
        } // Handle any errors that may have occurred.
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
